package com.hmall.api.config;


import com.hmall.api.fallback.ItemClientFallback;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {

    //注册关于商品远程调用客户端的fallback
    @Bean
    public ItemClientFallback itemClientFallback() {
        return new ItemClientFallback();
    }


    //注册feign日志级别
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    //feign请求拦截器
    @Bean
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                //获取线程中的用户id
                Long userId = UserContext.getUser();
                if(userId != null){
                    //设置到feign请求头
                    requestTemplate.header("user-info", userId.toString());
                }
            }
        };
    }

}
